﻿Option Explicit On
Option Strict On

Imports System
Imports System.Runtime.InteropServices
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Serialization
Imports System.Management
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
Imports Microsoft.SharePoint.WebPartPages
Imports Microsoft.SharePoint.Utilities
Imports Microsoft.VisualBasic



Namespace SharepointServerInformationWP

    <Guid("1f73a422-ef35-4f5e-be42-5f6dcacd0cd8")> _
    Public Class ServerInformation
        Inherits System.Web.UI.WebControls.WebParts.WebPart

        Public Sub New()
        End Sub

        Protected Overrides Sub CreateChildControls()
            MyBase.CreateChildControls()
            Dim tbl As New Table
            Dim tRow1 As New TableRow
            Dim tRow2 As New TableRow
            Dim tRow3 As New TableRow
            Dim tRow4 As New TableRow
            Dim tRow5 As New TableRow

            Dim tR1C1 As New TableCell
            Dim tR1C2 As New TableCell
            Dim tR2C1 As New TableCell
            Dim tR2C2 As New TableCell
            Dim tR3C1 As New TableCell
            Dim tR3C2 As New TableCell
            Dim tR4C1 As New TableCell
            Dim tR4C2 As New TableCell
            Dim tR5C1 As New TableCell
            Dim tR5C2 As New TableCell

            tR1C2.CssClass = "ms-formbody"
            tR2C2.CssClass = "ms-formbody"
            tR3C2.CssClass = "ms-formbody"
            tR4C2.CssClass = "ms-formbody"
            tR5C2.CssClass = "ms-formbody"
            tR1C1.CssClass = "ms-formlabel"
            tR2C1.CssClass = "ms-formlabel"
            tR3C1.CssClass = "ms-formlabel"
            tR4C1.CssClass = "ms-formlabel"
            tR5C1.CssClass = "ms-formlabel"
            tRow1.Cells.Add(tR1C1)
            tRow1.Cells.Add(tR1C2)
            tRow2.Cells.Add(tR2C1)
            tRow2.Cells.Add(tR2C2)
            tRow3.Cells.Add(tR3C1)
            tRow3.Cells.Add(tR3C2)
            tRow4.Cells.Add(tR4C1)
            tRow4.Cells.Add(tR4C2)
            tRow5.Cells.Add(tR5C1)
            tRow5.Cells.Add(tR5C2)

            tbl.Rows.Add(tRow1)
            tbl.Rows.Add(tRow2)
            tbl.Rows.Add(tRow3)
            tbl.Rows.Add(tRow4)
            tbl.Rows.Add(tRow5)

            tR1C1.Text = "Server Serving Page: "
            tR2C1.Text = "Requested by: "
            tR3C1.Text = "From client host: "
            tR4C1.Text = "Using: "
            tR5C1.Text = "Content Database"
            ' TODO: add custom rendering code here.
            Dim label As New Label()
            label.Text = Page.Server.MachineName & " at " & Page.Request.PhysicalApplicationPath
            tR1C2.Controls.Add(label)
            Dim label2 As New Label()

            label2.Text = Page.Request.LogonUserIdentity.Name
#If CONFIG = "Demo" Then
            label2.Text = "Domain\TestUser"
#End If
            tR2C2.Controls.Add(label2)
            Dim label3 As New Label()
            label3.Text = Page.Request.UserHostAddress
            tR3C2.Controls.Add(label3)
            Dim Label4 As New Label()
            Label4.Text = Page.Request.UserAgent
            tR4C2.Controls.Add(Label4)


            Dim mysite = New SPSite(SPUtility.GetPageUrlPath(Context))

            tR5C2.Text = mysite.ContentDatabase.Name & " via " & mysite.ContentDatabase.DatabaseConnectionString

            Me.Controls.Add(tbl)

            Dim label5 As New Label
            label5.Text = "<h3>Machine Instrumentation</h3>"
            Me.Controls.Add(label5)

            Dim options As New System.Management.ConnectionOptions
            Dim scope As New ManagementScope("\\localhost\root\cimv2", options)
            scope.Connect()
            ' build OS data
            Dim query As New ObjectQuery("Select * from Win32_OperatingSystem")
            Dim query2 As New SelectQuery("Select * from Win32_LogicalDisk Where DriveType = 3")
            Dim Searcher As New ManagementObjectSearcher(scope, query)
            Dim Searcher1 As New ManagementObjectSearcher(scope, query2)
            Dim querycollection As ManagementObjectCollection = Searcher.Get()
            Dim queryCollection1 As ManagementObjectCollection = Searcher1.Get()
            Dim tbl2 As New Table

            For Each m In querycollection
                Dim tRow11 As New TableRow
                Dim tR1C11 As New TableCell
                Dim tR1C21 As New TableCell
                Dim tRow1a As New TableRow
                Dim tR1C1a As New TableCell
                Dim tR1C2a As New TableCell
                Dim tRow1b As New TableRow
                Dim tR1C1b As New TableCell
                Dim tR1C2b As New TableCell
                Dim tRow1c As New TableRow
                Dim tR1C1c As New TableCell
                Dim tR1C2c As New TableCell
                tRow11.Cells.Add(tR1C11)
                tRow11.Cells.Add(tR1C21)
                tRow1a.Cells.Add(tR1C1a)
                tRow1a.Cells.Add(tR1C2a)
                tRow1b.Cells.Add(tR1C1b)
                tRow1b.Cells.Add(tR1C2b)
                tRow1c.Cells.Add(tR1C1c)
                tRow1c.Cells.Add(tR1C2c)
                tR1C11.Text = "Computer Name :"
                tR1C11.CssClass = "ms-formlabel"
                tR1C21.Text = m.Item("csname").ToString
                tR1C21.CssClass = "ms-formbody"
                tbl2.Controls.Add(tRow11)
                tR1C1a.Text = "Operating System :"
                tR1C1a.CssClass = "ms-formlabel"
                tR1C2a.Text = m.Item("Caption").ToString
                tR1C2a.CssClass = "ms-formbody"
                tbl2.Controls.Add(tRow1a)
                tR1C1b.Text = "Version :"
                tR1C1b.CssClass = "ms-formlabel"
                tR1C2b.Text = m.Item("Version").ToString
                tR1C2b.CssClass = "ms-formbody"
                tbl2.Controls.Add(tRow1b)
                tR1C1c.Text = "Physical Memory :"
                tR1C1c.CssClass = "ms-formlabel"
                Dim lngPhysicalMem = CLng(m.Item("TotalVisibleMemorySize"))
                Dim lngFreeMem = CLng(m.Item("FreePhysicalMemory"))
                Dim dblPercentFree As Double = 1 - (lngFreeMem / lngPhysicalMem)
                lngPhysicalMem = CLng(lngPhysicalMem * 0.0009765625)
                tR1C2c.Text = CStr(lngPhysicalMem) & "M at " & Format(dblPercentFree, "P") & " utilization"
                tR1C2c.CssClass = "ms-formbody"
                tbl2.Controls.Add(tRow1c)

            Next
            Me.Controls.Add(tbl2)
            Dim label6 As New Label
            label6.Text = "<h3>Drive Information</h3>"
            Me.Controls.Add(label6)
            Dim tbl3 As New Table
            Dim tRHow11 As New TableRow
            Dim tRH1C11 As New TableCell
            Dim tRH1C21 As New TableCell
            Dim tRH1C31 As New TableCell
            Dim tRH1C41 As New TableCell
            Dim tRH1C51 As New TableCell
            tRHow11.Cells.Add(tRH1C11)
            tRHow11.Cells.Add(tRH1C21)
            tRHow11.Cells.Add(tRH1C31)
            tRHow11.Cells.Add(tRH1C41)
            tRHow11.Cells.Add(tRH1C51)
            tRH1C11.Text = "Drive Name"
            tRH1C21.Text = "Volume Name"
            tRH1C31.Text = "Free Space"
            tRH1C41.Text = "Drive Size"
            tRH1C51.Text = "% Free"
            tRH1C11.CssClass = "ms-formlabel"
            tRH1C21.CssClass = "ms-formlabel"
            tRH1C31.CssClass = "ms-formlabel"
            tRH1C41.CssClass = "ms-formlabel"
            tRH1C51.CssClass = "ms-formlabel"
            tbl3.Controls.Add(tRHow11)
            For Each m In queryCollection1
                Dim tRow11 As New TableRow
                Dim tR1C11 As New TableCell
                Dim tR1C21 As New TableCell
                Dim tR1C31 As New TableCell
                Dim tR1C41 As New TableCell
                Dim tR1C51 As New TableCell
                tRow11.Cells.Add(tR1C11)
                tRow11.Cells.Add(tR1C21)
                tRow11.Cells.Add(tR1C31)
                tRow11.Cells.Add(tR1C41)
                tRow11.Cells.Add(tR1C51)
                tR1C11.CssClass = "ms-formbody"
                tR1C21.CssClass = "ms-formbody"
                tR1C31.CssClass = "ms-formbody"
                tR1C41.CssClass = "ms-formbody"
                tR1C51.CssClass = "ms-formbody"
                tR1C11.Text = m("Name").ToString
                tR1C21.Text = m("VolumeName").ToString
                tR1C31.Text = Format((CLng(m("FreeSpace")) / 1073741824), "##.#") & "G"
                tR1C41.Text = Format((CLng(m("Size")) / 1073741824), "##.#") & "G"
                tR1C51.Text = Format(CLng(m("FreeSpace")) / CLng(m("Size")), "p")
                tbl3.Controls.Add(tRow11)
            Next
            Me.Controls.Add(tbl3)
        End Sub

    End Class

End Namespace
